import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/system/tenant'

const api = {

    /**
     * 新增租户定义
     * @param tenant
     * @returns {Promise<*|undefined>}
     */
    createTenant: (tenant) =>
      axiosClient().post(`${requestUrl}/addInfo`, tenant),

    /**
     * 根据ID删除租户定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteTenantById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新租户定义
     * @param id
     * @param tenant
     * @returns {Promise<*|undefined>}
     */
    updateTenantFull: (id, tenant) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, tenant),

    /**
     * 根据ID部分更新租户定义
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateTenantPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询租户定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getTenantById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询租户定义
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getTenantPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询租户定义
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getTenantList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.tenantCode) {
            params.append('tenantCode', businessParam.tenantCode)
        }
        if (businessParam.tenantName) {
            params.append('tenantName', businessParam.tenantName)
        }
        if (businessParam.contactUser) {
            params.append('contactUser', businessParam.contactUser)
        }
        if (businessParam.contactMobile) {
            params.append('contactMobile', businessParam.contactMobile)
        }
        if (businessParam.createUserId) {
            params.append('createUserId', businessParam.createUserId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
